package com.ymatou.iapi.fileupload.testcase;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.coupon.parameter.old.ShoppingInfo;
import com.ymatou.iapi.couponmanage.old.parameter.CreateCouponBean;
import com.ymatou.iapi.fileupload.parameter.DeleteDFSFileBean;
import com.ymatou.iapi.fileupload.service.DeleteDFSFileCall;
import com.ymatou.iapi.fileupload.service.UploadCall;
import com.ymatou.promotion.api.parameter.CouponBatchBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;
/**
*删除图片
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_DeleteDFSFile{
	private static DeleteDFSFileBean deletedfsfileBean;
	private static DeleteDFSFileCall deletedfsfileCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("删除图片");
	}

	@Before
	public void caseUp() {
		deletedfsfileBean=new DeleteDFSFileBean();
		deletedfsfileCall=new DeleteDFSFileCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(deletedfsfileCall.getOpurl());
		
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DeleteDFSFile_001() {
		Logger.start(true, "删除product图片成功,图片删除开始...");
		try {
			String turl = getUrl();			
			Logger.comment("删除上传文件:" + turl);
			deletedfsfileBean.setFileUrl(turl);
			deletedfsfileCall.setData(deletedfsfileBean);
			deletedfsfileCall.callService();
			Logger.verifyEquals(ECode.SUCCESS, deletedfsfileCall.getStatus(), "验证Status");
			Logger.verifyEquals("操作成功", deletedfsfileCall.getMsg(), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DeleteDFSFile_002() {
		Logger.start(false, "反例-图片不存在时有相应报错");
		try {
			String turl = "http://file.iapi.ymatou.com/G01/M00/72/rBBlD1d7GBOAFvn8AAvea_OGt2M845.jpg";			
			Logger.comment("删除上传文件:" + turl);
			deletedfsfileBean.setFileUrl(turl);    //传参
			deletedfsfileCall.setData(deletedfsfileBean);   //组成请求串
			deletedfsfileCall.callService();    //模拟请求
			Logger.verifyEquals(ECode.ERROR500, deletedfsfileCall.getStatus(), "验证Status");
			Logger.verifyIsContains("操作错误", deletedfsfileCall.getMsg(), "验证Msg");
			Logger.verifyEquals("false", deletedfsfileCall.getIsSuccess(), "验证IsSuccess");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_DeleteDFSFile_003() {
		Logger.start(true, "批量删除product图片成功,图片删除开始...");
		try {
			ArrayList<String> list = TestDataManager.getLines( System.getProperty("user.dir") + File.separator +"tempfile");
			for (String string : list) {
				Logger.comment("删除上传文件:" + string);
				new Thread(()->{
					deletedfsfileCall=new DeleteDFSFileCall();
					deletedfsfileBean.setFileUrl(string);
					deletedfsfileCall.setData(deletedfsfileBean);
					try {
						deletedfsfileCall.callService();
					} catch (Exception e) {
						Logger.debug("error:"+e.getMessage());
					}
				}).start();
//				deletedfsfileCall=new DeleteDFSFileCall();
//				deletedfsfileBean.setFileUrl(string);
//				deletedfsfileCall.setData(deletedfsfileBean);
//				deletedfsfileCall.callService();
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	/**
	 * 修改url地址，本case专用 用于在原来的地址文件名后加后缀
	 * 
	 * @return 修改后的地址
	 */

	private static String changeUrl(String url, String suffix) {
		return url.substring(0, url.lastIndexOf("_")) + suffix + url.substring(url.lastIndexOf("."), url.length());
	}
	
	private String getUrl() throws ClientProtocolException, IOException{
		Logger.comment("开始上传文件");
		UploadCall uploadcall=new UploadCall();
		String turl = "";
		String fliename = System.getProperty("user.dir") + File.separator + "resource" + File.separator
				+ "Penguins.jpg";
		Logger.comment("添加上传文件:" + fliename);
		uploadcall.addPart("file", fliename);
		uploadcall.callService();
		return uploadcall.getFileUrl();
	}
	
		
}